package com.sojson.project.sys.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.sys.user.controller.param.verify.UserMerchantParamVerify;
import com.sojson.project.sys.user.entity.dto.UserMerchantDto;
import com.sojson.project.sys.user.service.UserMerchantService;
import com.sojson.result.Result;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.token.interfaces.BaseUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商户个人信息控制器
 * 
 * @author liu
 * @date 2022-12-05
 */
@Api(tags = "商户个人信息控制器")
@RestController
@RequestMapping("/sys/user/merchant/profile")
public class UserMerchantProfileController extends BaseUserProfileController {

    @Autowired
    private UserMerchantService userMerchantService;

    /**
     * 修改个人信息
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @PutMapping("/updateInfo")
    @PreAuthorize("@ss.login()")
    @ApiOperation("修改个人信息")
    public Result<Boolean> updateInfo(@RequestBody UserMerchantDto dto) throws Exception {
        dto.setId(TokenUtil.getUserId());
        // 校验参数
        Result<Boolean> paramResult = UserMerchantParamVerify.updateInfo(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(userMerchantService.updateInfo(dto));
    }

    @InitBinder
    @Override
    protected void initUserService() {
        setUserService((BaseUserService)userMerchantService);
    }

}